//
//  YAYIMutiPracticeHospitalCell.m
//  YAYIDoctor
//
//  Created by Veer on 16/6/28.
//
//

#import "YAYIMutiPracticeHospitalCell.h"
#import "YAYIRecruitHospitalModel.h"


@interface YAYIMutiPracticeHospitalCell()

@property (nonatomic, weak) UILabel *nameLbl;
@property (nonatomic, weak) UILabel *addressLbl;
@property (nonatomic, weak) UILabel *distanceLbl;
@property (nonatomic, weak) UIView *lineView;
@property (nonatomic, weak) UILabel *cooperationTitleLbl;
@property (nonatomic, weak) UILabel *cooperationDetailLbl;
@property (nonatomic, weak) UIView *lineView2;

@end


@implementation YAYIMutiPracticeHospitalCell

- (void)setModel:(YAYIRecruitHospitalModel *)model
{
    _model = model;
    
    if ([model.avatar rangeOfString:@"/"].location == NSNotFound) {
        self.imgView.image = [UIImage imageNamed:model.avatar];
    }else{
        NSString *urlStr = [NSString stringWithFormat:@"%@%@",API, model.avatar];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"home_showImg1"] options:SDWebImageRefreshCached];
    }
    
    self.nameLbl.text = model.hospitalname;
    self.addressLbl.text = model.hosaddress;
    self.distanceLbl.text = [self getDistanceStringByDistance:model.distance];
    self.cooperationDetailLbl.text = model.jobtitle;
}

+ (instancetype)cellWith:(UITableView *)tableview
{
    static NSString *cellID = @"YAYIMutiPracticeHospitalCell";
    
    YAYIMutiPracticeHospitalCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYIMutiPracticeHospitalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.containView.transform =  CGAffineTransformIdentity;
    
    return cell;
}



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self setupSubview];
        
    }
    return self;
}


- (void)setupSubview
{
    
    UIView *containView = [[UIView alloc] init];
    containView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:containView];
    self.containView = containView;
    
    // 图标
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"photo_sample_01"];
    imageView.backgroundColor = [UIColor whiteColor];
//    imageView.contentMode = UIViewContentMode;
    [self.containView addSubview:imageView];
    self.imgView = imageView;
    
    // 医院名
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.text = @" ";
    nameLbl.font = [UIFont systemFontOfSize:17];
    [self.containView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    // 地址
    UILabel *addressLbl = [[UILabel alloc] init];
    addressLbl.text = @" ";
    addressLbl.font = [UIFont systemFontOfSize:15];
    addressLbl.textColor = YAYIColor(116, 113, 113);
    [self.containView addSubview:addressLbl];
    self.addressLbl = addressLbl;
    
    // 距离
    UILabel *distanceLbl = [[UILabel alloc] init];
    distanceLbl.text = @" ";
    distanceLbl.font = [UIFont systemFontOfSize:15];
    distanceLbl.textColor = YAYIColor(116, 113, 113);
    [self.containView addSubview:distanceLbl];
    self.distanceLbl = distanceLbl;
    
    // 线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = YAYIColor(0xdd, 0xdd, 0xdd);
    [self.containView addSubview:lineView];
    self.lineView = lineView;
    
    // 标题
    UILabel *cooperationTitleLbl = [[UILabel alloc] init];
    cooperationTitleLbl.text = @"合作信息";
    cooperationTitleLbl.font = [UIFont systemFontOfSize:16];
    cooperationTitleLbl.textColor = YAYIColor(96, 167, 237);
    [self.containView addSubview:cooperationTitleLbl];
    self.cooperationTitleLbl = cooperationTitleLbl;
    
    // 左边兼职信息label
    UILabel *cooperationDetailLbl = [[UILabel alloc] init];
    cooperationDetailLbl.text = @" ";
    cooperationDetailLbl.numberOfLines = 0;
    cooperationDetailLbl.font = [UIFont systemFontOfSize:14];
    cooperationDetailLbl.textColor = YAYIColor(141, 142, 143);
    [self.containView addSubview:cooperationDetailLbl];
    self.cooperationDetailLbl = cooperationDetailLbl;
    
    // 线
    UIView *lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = YAYIColor(0xee, 0xdd, 0xee);
    [self.containView addSubview:lineView2];
    self.lineView2 = lineView2;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置frame
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView).offset(-15);
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_width);
        make.height.mas_equalTo(310 * YAYIScreenScale);
    }];
    
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
    }];
    
    [self.addressLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLbl);
        make.right.mas_equalTo(-70);
        make.top.mas_equalTo(self.nameLbl.mas_bottom).offset(5);
    }];
    
    [self.distanceLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.addressLbl);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(SCREEN_width);
        make.left.mas_equalTo(0);
        make.top.equalTo(self.addressLbl.mas_bottom).offset(8);
    }];
    
    [self.cooperationTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(5);
    }];
    
    [self.cooperationDetailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.cooperationTitleLbl.mas_bottom).offset(5);
        make.right.mas_equalTo(-15);
        make.bottom.mas_lessThanOrEqualTo(-10);
    }];
    
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(SCREEN_width);
        make.left.mas_equalTo(0);
        make.bottom.equalTo(self.containView.mas_bottom);
    }];
}


- (void)startAnimationWithDelay:(CGFloat)delayTime
{
    self.containView.transform =  CGAffineTransformIdentity;
    self.contentView.transform =  CGAffineTransformMakeTranslation(SCREEN_width, 0);
    [UIView animateWithDuration:1. delay:delayTime usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
        self.contentView.transform = CGAffineTransformIdentity;
    } completion:NULL];
}


- (void)startQuitAnimationWithDelay:(CGFloat)delayTime
{
    
    [UIView animateWithDuration:1. delay:delayTime usingSpringWithDamping:0.6 initialSpringVelocity:0 options:0 animations:^{
        
        self.containView.transform =  CGAffineTransformMakeTranslation(SCREEN_width, 0);
    } completion:NULL];
}


#pragma mark - 工具方法
- (NSString *)getDistanceStringByDistance:(NSInteger)distance
{
    NSString *distanceString = @"";
    
    if (distance < 1000){
        
        distanceString = [NSString stringWithFormat:@"< %.1fkm", 1.0];
    }else if (distance < 10000){
        
        CGFloat km = distance / 1000.0;
        distanceString = [NSString stringWithFormat:@"< %0.1fkm", km];
    }else{
        
        NSInteger km = (distance + 500) / 1000;
        distanceString = [NSString stringWithFormat:@"< %zdkm", km + 1];
    }
    
    
    return distanceString;
}


@end
